{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Data Imputation with Last Seen Value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/xujing98/analytics-zoo/zoo/python_packages/sources/bigdl/util/engine.py:41: UserWarning: Find both SPARK_HOME and pyspark. You may need to check whether they match with each other. SPARK_HOME environment variable is set to: /home/xujing98/spark-2.4.3-bin-hadoop2.7, and pyspark is found in: /home/xujing98/anaconda3/envs/automl/lib/python3.6/site-packages/pyspark/__init__.py. If they are unmatched, please use one source only to avoid conflict. For example, you can unset SPARK_HOME and use pyspark only.\n",
      "  warnings.warn(warning_msg)\n",
      "/home/xujing98/analytics-zoo/pyzoo/zoo/util/engine.py:42: UserWarning: Find both SPARK_HOME and pyspark. You may need to check whether they match with each other. SPARK_HOME environment variable is set to: /home/xujing98/spark-2.4.3-bin-hadoop2.7, and pyspark is found in: /home/xujing98/anaconda3/envs/automl/lib/python3.6/site-packages/pyspark/__init__.py. If they are unmatched, you are recommended to use one source only to avoid conflict. For example, you can unset SPARK_HOME and use pyspark only.\n",
      "  warnings.warn(warning_msg)\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import sklearn.metrics as metrics\n",
    "\n",
    "from zoo.zouwu.preprocessing.impute.LastFill import LastFill"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "raw_df = pd.read_csv(\"data/data.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame(pd.to_datetime(raw_df.StartTime))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "raw_df.AvgRate.str[-4:].unique()\n",
    "# Unify AvgRate value\n",
    "df['AvgRate'] = raw_df.AvgRate.apply(lambda x: float(x[:-4]) if x.endswith(\"Mbps\") else float(x[:-4]) * 1000)\n",
    "df[\"total\"] = raw_df[\"total\"]\n",
    "df.set_index(\"StartTime\", inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>AvgRate</th>\n",
       "      <th>total</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>StartTime</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-01-01 00:00:00</th>\n",
       "      <td>306.23</td>\n",
       "      <td>275605455598</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-01 02:00:00</th>\n",
       "      <td>285.03</td>\n",
       "      <td>256527692256</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-01 04:00:00</th>\n",
       "      <td>247.39</td>\n",
       "      <td>222652190823</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-01 06:00:00</th>\n",
       "      <td>211.55</td>\n",
       "      <td>190396029658</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-01 08:00:00</th>\n",
       "      <td>234.82</td>\n",
       "      <td>211340468977</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     AvgRate         total\n",
       "StartTime                                 \n",
       "2018-01-01 00:00:00   306.23  275605455598\n",
       "2018-01-01 02:00:00   285.03  256527692256\n",
       "2018-01-01 04:00:00   247.39  222652190823\n",
       "2018-01-01 06:00:00   211.55  190396029658\n",
       "2018-01-01 08:00:00   234.82  211340468977"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "sampled_df = df[\"2019-01-01 00:00:00\":\"2019-06-30 23:00:00\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "no. of n/a values:\n",
      "AvgRate    0\n",
      "total      0\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "full_idx = pd.date_range(start=sampled_df.index.min(), end=sampled_df.index.max(), freq='2H')\n",
    "sampled_df = df.reindex(full_idx)\n",
    "print(\"no. of n/a values:\")\n",
    "print(sampled_df.isna().sum())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>AvgRate</th>\n",
       "      <th>total</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-01-01 00:00:00</th>\n",
       "      <td>271.53</td>\n",
       "      <td>244373500495</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-01-01 02:00:00</th>\n",
       "      <td>208.49</td>\n",
       "      <td>187638154842</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-01-01 04:00:00</th>\n",
       "      <td>175.88</td>\n",
       "      <td>158293382443</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-01-01 06:00:00</th>\n",
       "      <td>229.20</td>\n",
       "      <td>206284366389</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-01-01 08:00:00</th>\n",
       "      <td>264.95</td>\n",
       "      <td>238459494810</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-06-30 14:00:00</th>\n",
       "      <td>618.90</td>\n",
       "      <td>557013671978</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-06-30 16:00:00</th>\n",
       "      <td>533.31</td>\n",
       "      <td>479975450009</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-06-30 18:00:00</th>\n",
       "      <td>475.86</td>\n",
       "      <td>428273046817</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-06-30 20:00:00</th>\n",
       "      <td>448.12</td>\n",
       "      <td>403312442571</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-06-30 22:00:00</th>\n",
       "      <td>512.81</td>\n",
       "      <td>461531722122</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2172 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                     AvgRate         total\n",
       "2019-01-01 00:00:00   271.53  244373500495\n",
       "2019-01-01 02:00:00   208.49  187638154842\n",
       "2019-01-01 04:00:00   175.88  158293382443\n",
       "2019-01-01 06:00:00   229.20  206284366389\n",
       "2019-01-01 08:00:00   264.95  238459494810\n",
       "...                      ...           ...\n",
       "2019-06-30 14:00:00   618.90  557013671978\n",
       "2019-06-30 16:00:00   533.31  479975450009\n",
       "2019-06-30 18:00:00   475.86  428273046817\n",
       "2019-06-30 20:00:00   448.12  403312442571\n",
       "2019-06-30 22:00:00   512.81  461531722122\n",
       "\n",
       "[2172 rows x 2 columns]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sampled_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "last_fill = LastFill()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "no. of n/a values:\n",
      "AvgRate    327\n",
      "total      327\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "full_idx = pd.date_range(start=df.index.min(), end=df.index.max(), freq='2H')\n",
    "df1 = df.reindex(full_idx)\n",
    "print(\"no. of n/a values:\")\n",
    "print(df1.isna().sum())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "filled_df = last_fill.impute(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "no. of n/a values:\n",
      "AvgRate    0\n",
      "total      0\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "full_idx = pd.date_range(start=df.index.min(), end=df.index.max(), freq='2H')\n",
    "fill_df = filled_df.reindex(full_idx)\n",
    "print(\"no. of n/a values:\")\n",
    "print(filled_df.isna().sum())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "mse_10 = last_fill.evaluate(sampled_df, 0.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2004.2239946593002, 1.6857280722708464e+21]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mse_10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "mse_50 = last_fill.evaluate(sampled_df, 0.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[28298.18132730203, 2.4879356854775073e+22]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mse_50"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
